//
//  B_AttentionViewController.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/19.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_AttentionViewController.h"
#import "B_FansTableViewCell.h"
#import "B_AttentionListModel.h"
#import "B_UserMainPageVC.h"

@interface B_AttentionViewController ()
@property (nonatomic, strong) BaseTableView * mainView;
@end

@implementation B_AttentionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"我的关注"];
    self.view = self.mainView;
    [self getData:YES];
}


- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_FansTableViewCell" bundle:nil] forCellReuseIdentifier:@"B_FansTableViewCell"];
        
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.dataArray.count;
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 72;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_AttentionListModel * model = weSelf.dataArray[indexPath.row];
            B_FansTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"B_FansTableViewCell" forIndexPath:indexPath];
            cell.B_userName.text = model.nickName;
            cell.B_userSignature.text = model.signature;
            [cell.B_userImageView sd_setImageWithURL:[NSURL URLWithString:model.portrait]];
            
            if ([model.userType isEqualToString:@"2"]) {
                cell.B_userTypeImageView.hidden = NO;
                cell.B_userTypeImageView.image = [UIImage imageNamed:@"B_personal_center_real_name_user_icon"];
            } else if ([model.userType isEqualToString:@"3"]) {
                /// 商家
                cell.B_userTypeImageView.hidden = NO;
                cell.B_userTypeImageView.image = [UIImage imageNamed:@"B_personal_center_merchants_icon"];
            } else {
                cell.B_userTypeImageView.hidden = YES;
            }
            
            if ([model.isFollow isEqualToString:@"1"]) {
                cell.B_attentionButton.backgroundColor = [UIColor color_text_deep_gray];
                [cell.B_attentionButton setTitle:@"已关注" forState:UIControlStateNormal];
            } else {
                cell.B_attentionButton.backgroundColor = [UIColor color_pink];
                [cell.B_attentionButton setTitle:@"关注" forState:UIControlStateNormal];
            }
            
            cell.attentionAction = ^{
                /// TODO:关注操作
                [weSelf attentionActionWithUserId:model];
            };
            
            return cell;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            B_AttentionListModel * model = weSelf.dataArray[indexPath.row];
            B_UserMainPageVC * userMain = [[B_UserMainPageVC alloc] init];
            userMain.B_userId = model.userId;
            [weSelf pushNextViewController:userMain];
        };
        
        [_mainView setTableViewHeaderFresh:^{
            [weSelf getData:YES];
        }];
        [_mainView setTableViewFooterFresh:^{
            [weSelf getData:NO];
        }];
        
    }
    return _mainView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) getData:(BOOL) isRefresh {
    
    if (isRefresh) {
        self.index = 1;
    } else {
        self.index ++;
    }
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/userFollow/selectUserFollow/%@/%ld/10", getUserId(), (long)self.index] parameters:nil isCache:isRefresh succeed:^(id data) {
        [weSelf.mainView endRefresh];
        if ([data[@"code"] isEqualToString:@"20010"]) {
            if (isRefresh) {
                [weSelf.dataArray removeAllObjects];
            }
            for (NSDictionary * dictionary in data[@"values"]) {
                B_AttentionListModel * model = [B_AttentionListModel mj_objectWithKeyValues:dictionary];
                [weSelf.dataArray addObject:model];
            }
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [weSelf.mainView endRefresh];
        [KeyWindow makeToast:@"服务器异常"];
    }];
    
}



/**
 TODO: 执行关注操作
 
 @param model 我的关注的模型
 */
- (void) attentionActionWithUserId:(B_AttentionListModel *) model {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/userFollow/addDelUserFollow/%@/%@", getUserId(), model.userId] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20005"]) {
            if ([model.isFollow isEqualToString:@"1"]) {
                model.isFollow = @"2";
            } else {
                model.isFollow = @"1";
            }
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
